package com.example.friend.controller;


import com.example.core.controller.BaseController;
import com.example.core.domain.LoginUser;
import com.example.core.domain.R;
import com.example.core.enums.ResultCode;
import com.example.friend.domain.dto.UserDTO;
import com.example.friend.domain.dto.UserUpdateDTO;
import com.example.friend.domain.vo.UserVO;
import com.example.friend.service.IUserService;
import com.example.scurity.service.TokenService;
import com.example.scurity.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "UserControllerApi", description = "用户API")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    public R<?> login(@RequestBody UserDTO loginDTO) {
        LoginUser loginUser = userService.login(loginDTO);
        return R.ok(tokenService.createToken(loginUser));
    }

    @PostMapping("/code/login")
    public R<?> codeLogin(@RequestBody UserDTO loginDTO) {
        LoginUser loginUser = userService.codeLogin(loginDTO);
        return R.ok(tokenService.createToken(loginUser));
    }

    @PostMapping("/register")
    public R<?> register(@RequestBody UserDTO registerDTO) {
        if (!userService.checkUserInfoUnique(registerDTO)) {
            return R.fail("该账户已注册", ResultCode.AILED_USER_EXISTS);
        }
        if (!registerDTO.getPassword().equals(registerDTO.getConfirmPassword())) {
            return R.fail("两次输入密码不一致", ResultCode.FAILED);
        }
        return toAjax(userService.register(registerDTO));
    }

    /**
     * 发送短信验证码
     *
     * @param userDTO 用户信息
     * @return
     */
    @Operation(summary = "发送验证码")
    @PostMapping("/semiLogin/sendCode")
    public R<?> sendCode(@RequestBody UserDTO userDTO) {
        return toAjax(userService.sendCode(userDTO.getPhone()));
    }

    @Operation(summary = "查看用户详情")
    @GetMapping("/detail")
    public R<UserVO> detail(Long userId) {
        return R.ok(userService.detail(userId));
    }

    @Operation(summary = "查看用户基本信息")
    @GetMapping("/getSimpleUserInfo")
    public R<UserVO> getSimpleUserInfo(Long userId) {
        return R.ok(userService.getSimpleUserInfo(userId));
    }

    @Operation(summary = "编辑用户信息")
    @PutMapping("/edit")
    public R<?> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        if (userService.edit(userUpdateDTO)) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser == null) {
                return R.ok();
            }
            loginUser.setNickName(userUpdateDTO.getNickName());
            return toAjax(tokenService.refreshLoginUser(loginUser));
        }
        return R.fail("修改用户信息失败");
    }

    @Operation(summary = "上传用户头像")
    @PutMapping("/head-image/upload")
    public R<?> uploadHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toAjax(userService.uploadHeadImage(userUpdateDTO.getHeadImage()));
    }


    @PostMapping("/semiLogin/updatePasswd")
    public R<?> updatePasswd(@RequestBody UserDTO registerDTO) {
        return toAjax(userService.updatePasswd(registerDTO.getUserAccount()));
    }
}
